var Config = require('./config.js');
var QQMapWX = require('./qqmap-wx-jssdk.min.js');
var QQMapSdk;
import Helper from '@/utils/helper.js';
import Route from '@/utils/route.js';

// 微信小程序获取位置
const wechat = () => {
	QQMapSdk = new QQMapWX({
		key: Config.tencent_map.key
	})
	var this_latitude, this_longitude, map;
	return new Promise((resolve, reject) => {
		uni.authorize({
			scope: 'scope.userLocation',
			success(e) {
				// console.log('位置信息-有权限', e)
				uni.getLocation({
					type: 'gcj02',
					geocode: true,
					success: (res) => {
						this_latitude = res.latitude;
						this_longitude = res.longitude;
						// console.log("获取经纬度成功,this_latitude", this_latitude);
						// console.log("获取经纬度成功,this_longitude", this_longitude);
						// 解析地址
						QQMapSdk.reverseGeocoder({
							location: {
								latitude: this_latitude,
								longitude: this_longitude
							},
							success: async (res) => {
								let province = res.result.ad_info
									.province;
								let city = res.result.ad_info.city;
								let district = res.result.ad_info
									.district;
								let map = {
									latitude: this_latitude,
									longitude: this_longitude,
									city: city || '',
									district: district || '',
									city_code: '',
									district_code: ''
								}
								let res_city = await Route
									.city_find({
										cityName: map.city
									})
								let res_district = await Route
									.district_find({
										areaName: map.district,
										cityId: res_city.cityId
									})
								map.city_code = res_city.cityId
								map.district_code = res_district
									.areaId
								resolve(map);
								// console.log('腾讯地图地址解析-小程序', res);
								// console.log('省',province);
								// console.log('市',city);
							},
							fail: function(res) {
								console.log('定位失败原因', res)
								// uni.showToast({
								// 	title: '定位失败',
								// 	duration: 1500,
								// 	icon: "none"
								// })

								uni.showToast({
									title: '定位失败',
									icon: 'none',
								})
								reject();
								console.log('定位失败3', res);
							},
						})

					},
					fail: (e) => {
						console.log('uni.getLocation失败原因',e)
						// uni.showToast({
						// 	title: '已默认城市1 北京',
						// 	icon: 'none',
						// })
					},

				})
			},
			fail(e) {
				//设置地图默认信息
				let map = {
					latitude: 0,
					longitude: 0,
					city: '北京',
					district: '',
					city_code: 3,
					district_code: ''
				};
				uni.showModal({
					content: '为了给您提供更好的服务,建议打开位置功能权限',
					confirmText: "确认",
					cancelText: '取消',
					success: (res) => {
						if (res.confirm) {
							uni.openSetting({
								success: (res) => {
									console.log('为了给您提供更好的服务,建议打开位置功能权限')
									uni.navigateTo({
										url: '/pages/address_city/address_city'
									})
								},
								fail: (err) => {
									uni.showToast({
										title: '已默认城市2 北京',
										icon: 'none',
									})
								}
							})
						} else {
							uni.setStorageSync('map',map)
							uni.showToast({
								title: '小主,您未开启位置授权',
								icon: 'none',
							})
						}
					}
				})
				console.log('scope.userLocation没有权限', e)
			}
		})









	})
}
// H5获取位置
const H5 = async () => {
	let res = await Helper.jsonp('https://apis.map.qq.com/ws/location/v1/ip', {
		key: Config.tencent_map.key,
		output: "jsonp"
	});
	console.log('腾讯地图地址解析-H5', res);

	let map = {
		latitude: res.result.location.lat,
		longitude: res.result.location.lng,
		city: res.result.ad_info.city,
		district: res.result.ad_info.district,
		city_code: '',
		district_code: ''
	}
	return map;

}

export default {
	wechat,
	H5,

}
